import {defineStore} from 'pinia'
import http from "@/request/request"

export const useUserInfo = defineStore('userinfo', {
    state: () => {
        return {
            id: "",
            name: "",
            username: "",
            loginTime: 0,
            token: "",
            touxiang: "",
            desc: "",
        }
    },
    actions: {
        // 登录完成后，注册用户信息
        denglu(userdata) {
            this.id = userdata["id"]
            this.name = userdata["name"]
            this.username = userdata["username"]
            this.loginTime = new Date().getTime()
            this.token = userdata["token"]
            this.touxiang = userdata["touxiang"]
            this.desc = userdata["desc"]
        },
        // 注销用户信息
        zhuxiao() {
            this.id = ""
            this.name = ""
            this.username = ""
            this.loginTime = 0
            this.token = ""
            this.touxiang = ""
            this.desc = ""
            http.anylogin().then(res => {
                if (res.status === 200) {
                    console.log(res, "匿名登录的结果")
                    this.denglu(res.data.data)
                }
            })
        },
    },
    // pinia持久化
    persist: {
        key: 'kkkkyyy',    // 加密
        storage: window.sessionStorage, // 存储位置
    },
})
